package com.pdweb.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pdweb.dao.DivisoesDao;
import com.pdweb.vo.Divisoes;

/**
 * Servlet implementation class DivisoesConsultaServlet
 */
@WebServlet("/DivisoesConsultaServlet")
public class DivisoesConsultaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DivisoesConsultaServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		doService(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		doService(request, response);

	}

	protected void doService(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		try {
			String sql = null;
			Divisoes divisoes = new Divisoes();

			divisoes.setNomeDivisao(request.getParameter("ST_NOMEDIVISAO_DIV"));
			divisoes.setNomeResponsavel(request
					.getParameter("ST_RESPONSAVEL_DIV"));

			DivisoesDao divisoesDao = new DivisoesDao();
			
			String buscaNumeros = divisoes.getNomeDivisao();
				
			if (divisoes.getNomeDivisao() != null) {

				Pattern p = Pattern.compile("[0-9]{"+ buscaNumeros.length() +"}");   
				Matcher m = p.matcher(buscaNumeros);
				if (m.find()) { 			
					sql = "select id_divisao_div, empresas.st_nome_emp, st_nomedivisao_div, st_responsavel_div from divisoes inner join empresas on divisoes.empresas_id_empresa_emp = empresas.id_empresa_emp where ID_DIVISAO_DIV = '"
							+ divisoes.getNomeDivisao() + "';";
				} else{
					sql = "select id_divisao_div, empresas.st_nome_emp, st_nomedivisao_div, st_responsavel_div from divisoes inner join empresas on divisoes.empresas_id_empresa_emp = empresas.id_empresa_emp where st_nomedivisao_div LIKE '%"
						+ divisoes.getNomeDivisao() + "%' or st_responsavel_div LIKE '%"+divisoes.getNomeDivisao()+"%';";
				}
				List<Divisoes> divisoes1 = divisoesDao.consultaDivisoes(sql);

				if (divisoes1 != null) {

					request.setAttribute("divisoesConsulta", divisoes1);

					getServletContext().getRequestDispatcher(
							"/consulta_divisao.jsp").forward(request, response);

				} else {
					request.setAttribute("erro", "sem valores");

					getServletContext().getRequestDispatcher(
							"/consulta_divisao.jsp").forward(request, response);
				}

			}

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
